package com.caac.core.service.impl;

import java.util.Date;
import java.util.List;

import com.caac.core.dao.UserDao;
import com.caac.core.dm.NormalUser;
import com.caac.core.dm.Role;
import com.caac.core.dm.User;
import com.caac.core.service.NotFoundException;
import com.caac.core.service.NotMatchException;
import com.caac.core.service.UserManager;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright AgileCentury 2004</p>
 * <p>Company: AgileCentury</p>
 * @author <a href="mailto:project@agilecentury.com">Project Group</a>
 * @version 1.0
 */

public class UserManagerImpl extends BaseManagerImpl implements UserManager {
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public User getUserById(String id) {
        return userDao.getUserById(id);
    }

    public User getUserByUsername(String username) {
        return userDao.getUserByUsername(username);
    }

    public User getUserByUsernameAndPassword(String username, String password) throws
            NotFoundException, NotMatchException {
        User result;
        try {
            result = userDao.getUserByUsername(username);
        } catch (Exception e) {
            throw new NotFoundException("User '" + username + "' not found.");
        }
        if (!result.isValidPassword(password)) {
            throw new NotMatchException("User '" + username +
                                        "' with wrong password.");
        }
        //result.setDlcs(result.getDlcs()+1);
        //userDao.setFlushModeAuto();
        //userDao.saveUser(result);
        //System.out.println(result.getDlcs());
        return result;
    }

    public List getAccessibleUsers() {
        return null;
    }

    public void saveUser(User user, String[] roleIds, User operator) throws
            NotFoundException {
        NormalUser nu = (NormalUser) user;
        nu.setUpdateTime(new Date());
        nu.setUpdator(operator.getRealName()); 
        if (nu.getId() == null) {
        	nu.setCreator(nu.getUpdator());
            nu.setCreateTime(nu.getUpdateTime());
        }
        nu.getRoles().clear();
        userDao.saveUser(nu);
//        userDao.execNoResultSql("delete from user_role where user_id ='" +
//                                user.getId() + "'");
        if (roleIds != null) {
            for (int i = 0; i < roleIds.length; i++) {
                String id = roleIds[i];
                if (id != null) {
                    Role role = userDao.getRoleById(id);
                    nu.addRole(role);
                }
            }
        }
        userDao.saveUser(nu);
    }

    public void saveUser(User user, User operator) {
        userDao.saveUser(user);
    }

    public void deleteUserById(String id, User operator) {
        User user = userDao.getUserById(id);
        //if (!operator.getUsername().equals(user.getUsername())) {
            userDao.deleteObject(user);
        //}
    }

    //��ɫ���?��
    public Role getRoleById(String id) {
        return userDao.getRoleById(id);
    }

    public List getAllRoles() {
        return userDao.getAllRoles();
    }

    public void saveRole(Role role, String[] userIds, User operator) {
        userDao.saveRole(role);
    }

    public void saveRoleStateById(String id, User operator) {
        Role role = userDao.getRoleById(id);
        userDao.saveRole(role);
    }

    public void deleteRoleById(String id, User operator) {
        userDao.deleteObjectById(Role.class, id);
    }

    //Ȩ�޴��?��
    public void saveMenus(String roleId, String[] menuKeys) {
        //Role role = userDao.getRoleById(roleId);
        //role.getMenus().clear();
        //userDao.saveRole(role);
        userDao.execNoResultSql("delete from permission where role_id ='" +
                                roleId + "'");
        if (menuKeys != null) {
            for (int i = 0; i < menuKeys.length; i++) {
                String menuKey = menuKeys[i];
                if (menuKey != null) {
                    //Menu menu = (Menu) userDao.getMenuByMenuCode(menuKey);
                    //role.addMenu(menu);
                    userDao.saveRoleMenu(roleId, menuKey);
                }
            }
        }
        //userDao.saveRole(role);
    }

    //�˵����?��
    public List getAllMenus() {
        return userDao.getAllMenus();
    }

    public List getMenusByUsername(String username) {
        return userDao.getMenusByUsername(username);
    }

    public List getMenusByUsername(String username,String sysNo){
        return userDao.getMenusByUsername(username,sysNo);
    }

    public List getPermissionsByRoleId(String roleId) {
        return userDao.getPermissionsByRoleId(roleId);
    }
}
